<?php

namespace app\api\controller\wanlshop;

use app\common\controller\Api;

use think\Hook;
use think\Exception;
use app\api\model\wanlshop\Shop;
use app\api\model\wanlshop\RoleUser;
use app\api\model\wanlshop\Hxq;
use app\api\model\wanlshop\HxqUser;

class Store extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    public function WriteOffData(){
        $shop = Shop::where('user_id',$this->auth->id)->find();
        if($shop){
            $shop_id = $shop->id;
        }else{
            $shop = RoleUser::where('user_id',$this->auth->id)->find();
            $shop_id = $shop->shop_id;
        }
        $shop = Shop::where('id',$shop_id)->find();
        $all = HxqUser::where('shop_id',$shop_id)->where('state','<>','cancellation')->count();
        $wait = HxqUser::where('shop_id',$shop_id)->where('state','normal')->count();
        $use = HxqUser::where('shop_id',$shop_id)->where('state','hidden')->count();
        $money = HxqUser::where('shop_id',$shop_id)->where('state','hidden')->sum('money');
        $data =[
            'all'=>$all,
            'wait'=>$wait,
            'use'=>$use,
            'money'=>$money,
            'shop'=>$shop
        ];
        $this->success('获取成功',$data);
    }
    public function all(){
        $shop_id = $this->request->post('shop_id');
        $all = Hxq::withCount(['hxquser'=>function($query){
            $query->where('state','<>','cancellation');
        }])
            ->where('shop_id',$shop_id)
            ->paginate(10);

        $this->success('获取成功',$all);
    }
    public function wait(){
        $shop_id = $this->request->post('shop_id');
        $wait = Hxq::withCount(['hxquser'=>function($query){
            $query->where('state','normal');
        }])
            ->where('shop_id',$shop_id)
            ->paginate(10);
        $wait->each(function ($item){
            $item['state'] = 'normal';
        });
        $this->success('获取成功',$wait);
    }
    public function use(){
        $shop_id = $this->request->post('shop_id');
        $use = Hxq::withCount(['hxquser'=>function($query){
            $query->where('state','hidden');
        }])
            ->where('shop_id',$shop_id)
            ->paginate(10);
        $use->each(function ($item){
            $item['state'] = 'hidden';
        });
        $this->success('获取成功',$use);
    }
}